Servlet & JSP by Budi Kurniawan
Author:Budi Kurniawan [Kurniawan, Budi]
Language: eng
Format: epub
Publisher: Brainy Software Inc.
Published: 0101-01-01T00:00:00+00:00
Example 2: Image Protector Filter
The Image Protector Filter in this example prevents an image from being downloaded by typing the image URL in the browser's Address box. An image in the application will only show if the link to the image is clicked on a page. The filter works by checking the value of the referer HTTP header. A null value means the current request has no referrer, in other words the resource is being requested directly by typing its URL. A resource with a non-null referer header will have the page of origin as its referrer. Note that the header name is spelled with one r between the second e and the third e.
The filter class, ImageProtectorFilter, is given in Listing 9.2. From the WebFilter annotation you know that the filter is applied to all resources having png, jpg, or gif extension.
Listing 9.2: The ImageProtectorFilter class
package filter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; @WebFilter(filterName = "ImageProtetorFilter", urlPatterns = { "*.png", "*.jpg", "*.gif" }) public class ImageProtectorFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void destroy() { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { System.out.println("ImageProtectorFilter"); HttpServletRequest httpServletRequest = (HttpServletRequest) request; String referrer = httpServletRequest.getHeader("referer"); System.out.println("referrer:" + referrer); if (referrer != null) { filterChain.doFilter(request, response); } else { throw new ServletException("Image not available"); } } }
The init and destroy methods are empty. The doFilter method reads the value of the referer header and either invokes the resource or throws an exception:
String referrer = httpServletRequest.getHeader("referer"); System.out.println("referrer:" + referrer); if (referrer != null) { filterChain.doFilter(request, response); } else { throw new ServletException("Image not available"); }
To test the filter, try opening the logo.png image by typing this URL in your browser's Address box:
http://localhost:8080/app09a/image/logo.png
You'll get an “Image not available” error message.
Now, invoke the image.jsp page:
http://localhost:8080/app09a/image.jsp
You should see the image. The reason why this works is because the image.jsp page contains this link that instructs the browser to download the image:
<img src='image/logo.png'/>
When the browser asked for the image for the link, it also sent the URL of the page (in this case, http://localhost:8080/app09a/image.jsp) to the server as the value of the referer header.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Ada | Ajax |
Assembly Language Programming | Borland Delphi |
C & C++ | C# |
CSS | Compiler Design |
Compilers | DHTML |
Debugging | Delphi |
Fortran | Java |
Lisp | Perl |
Prolog | Python |
RPG | Ruby |
Swift | Visual Basic |
XHTML | XML |
XSL |
Hello! Python by Anthony Briggs(9928)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9804)
The Mikado Method by Ola Ellnestam Daniel Brolund(9787)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8310)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7792)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7773)
Grails in Action by Glen Smith Peter Ledbrook(7705)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7568)
Windows APT Warfare by Sheng-Hao Ma(6958)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6690)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6558)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6426)
Kotlin in Action by Dmitry Jemerov(5076)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4323)
Solidity Programming Essentials by Ritesh Modi(4066)
Functional Programming in JavaScript by Mantyla Dan(4044)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3860)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3803)
The Ultimate iOS Interview Playbook by Avi Tsadok(3779)
